package com.luyou.framework.common.domain;


import com.alibaba.fastjson2.JSON;
import com.luyou.framework.common.constants.DefaultErrorCodeEnum;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;

import java.util.Date;

/**
 * 返回结果
 * @author luyou
 * @date 2024-6-27 20:59:27
 */
@Data
public class Result<T> {

    /**
     * 错误枚举
     */
    private Integer code;

    /**
     * 错误信息
     */
    private String msg;


    /**
     * 展示前端的tips
     */
    private String tips;

    /**
     * 返回错误数据
     */
    private  T data;

    /**
     * 时间搓
     */
    private Long timestamp;

    public Result(Integer code, String msg, String tips, T data ){
        this.code = code;
        this.msg = msg;
        this.tips = tips;
        this.data = data;
        this.timestamp = new Date().getTime();
    }


    /**
     * 返回正常
     * @return
     */
    public static <T> Result<T> success(){
        return success(null);
    }

    /**
     * 返回正常
     * @return
     */
    public static <T> Result<T> success(T data){
        return new Result<T>(DefaultErrorCodeEnum.SUCCESS.code, DefaultErrorCodeEnum.SUCCESS.msg, DefaultErrorCodeEnum.SUCCESS.msg, data);
    }

    /**
     * 返回异常
     * @param <T>
     * @return
     */
    public static <T> Result<T> error(){
        return error(DefaultErrorCodeEnum.ERROR.code, DefaultErrorCodeEnum.ERROR.msg, DefaultErrorCodeEnum.ERROR.msg, null);
    }


    /**
     * 返回异常
     * @return
     */
    public static <T> Result<T> error(String msg, String tips ){
        return new Result<T>(DefaultErrorCodeEnum.ERROR.code, msg, tips, null);
    }

    /**
     * 返回异常
     * @return
     */
    public static <T> Result<T> error(Integer code, String msg, String tips){
        return new Result<T>(code, msg, tips, null);
    }

    /**
     * 返回异常
     * @return
     */
    public static <T> Result<T> error(Integer code, String msg, String tips ,T data){
        return new Result<T>(code, msg, tips, data);
    }

    public String toJson(){
        return JSON.toJSONString(this);
    }

}
